package com.study.manage.mvc.org.judges;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.study.manage.dal.EvaluateJuryRepository;
import com.study.manage.dal.UserRepository;
import com.study.manage.domain.EvaluateJury;
import com.study.manage.domain.User;
import com.study.manage.dto.JsonResponseBean;
import com.study.manage.mvc.common.BaseController;
import com.study.manage.utils.MapUtils;

@Controller
@RequestMapping("org/evaluate/detail/pw")
public class JudgesController extends BaseController {
    
	@Autowired
    private UserRepository userRepository;
    @Autowired
    private EvaluateJuryRepository evaluateJuryRepository;
    @Autowired
    private JudgesService judgesService;
    
    @RequestMapping(path = "info", method = RequestMethod.GET)
    public String index(Long id , ModelMap model) {
    	
        model.put("number",judgesService.getBaseData(id));
        return "org/evaluate/judges";
    }

    @ResponseBody
    @RequestMapping(path = "getData", method = RequestMethod.GET)
    public JsonResponseBean getData(Long evaluateId,String orderType,
    		@PageableDefault(sort = {"createdAt"}, direction = Direction.DESC)Pageable pageable) {
    	Page<EvaluateJury> page = null;
    	if(StringUtils.isEmpty(orderType)) {
    		page = evaluateJuryRepository.getTableData(evaluateId,pageable);
    	} else if("dp".equals(orderType)) {
    		page = evaluateJuryRepository.getTableDataDesc(evaluateId,pageable);
    	}
    	Object obj = page.map(new Converter<EvaluateJury, Map<String, Object>>() {
    		@Override
    		public Map<String, Object> convert(EvaluateJury source) {
    			Map<String, Object> map = MapUtils.convert(source);
    			User user = userRepository.findOne(source.teacherId);
    			if(user!=null) map.put("name",user.name);
    			if(source.teacherId!=null) {
    				map.put("commentNumber",judgesService.getCommentNumber(evaluateId,source.teacherId));
    				map.put("postNumber",judgesService.getPostNumber(evaluateId,source.teacherId));
    				map.put("scoreNumber",judgesService.getScoreNumber(evaluateId,source.teacherId));
    			}
    			return map;
		      }
		  });
        return new JsonResponseBean(obj,true);
    }
    
    @ResponseBody
    @RequestMapping(path = "deleteJudges", method = RequestMethod.POST)
    public JsonResponseBean deleteJudges(Long evaluateId,Long id) {
    	
    	judgesService.deleteJudges(id);
        return new JsonResponseBean(judgesService.getBaseData(evaluateId),true);
    }

}
